package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import com.sky.result.Result;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Set;

/**
 * 菜品管理
 */
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品管理相关接口")
@Slf4j
public class DishController {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private DishService dishService;
    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @PostMapping()
    public Result save(@RequestBody DishDTO dishDTO){
        log.info("新增菜品：{}", dishDTO);    // 打印日志
        dishService.saveDishWithFlavor(dishDTO);
        //清理缓存数据
//        String key = "dish_"+dishDTO.getCategoryId();//可以用aop拦截器来实现缓存的清理
//        redisTemplate.delete(key);

        //清理缓存数据
        clearCache("dish_"+dishDTO.getCategoryId());
        return Result.success();
    }


    @GetMapping("/page")
    @ApiOperation(value = "分页查询菜品列表", notes = "分页查询菜品列表")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        log.info("分页查询菜品列表：{}", dishPageQueryDTO);
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * 菜品批量删除
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation(value = "删除菜品", notes = "删除菜品")
    public Result delete(@RequestParam List<Long>ids){//由MVC的请求参数绑定机制，将ids参数绑定到List<Long>ids中,帮我们解析数据
        log.info("删除菜品：{}", ids);
        dishService.deleteDishByIds(ids);

        //将所有的菜品缓存数据清理掉,dish_开头的key
//        Set keys = redisTemplate.keys("dish_*");
//        redisTemplate.delete(keys);
        clearCache("dish_*");
        return Result.success();
    }

    /**
     * 根据ID查询菜品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID查询菜品", notes = "根据ID查询菜品")
    public Result<DishVO>getById(@PathVariable("id") Long id){
        log.info("根据ID查询菜品：{}", id);
        DishVO dishVO = dishService.getDishByIdWithFlavor(id);
        return Result.success(dishVO);
    }


    @PutMapping
    @ApiOperation(value = "修改菜品", notes = "修改菜品")
    public Result update(@RequestBody DishDTO dishDTO){
        log.info("修改菜品：{}", dishDTO);
        dishService.updateDishWithFlavor(dishDTO);

        //redis缓存数据清理 有可能影响一份也可能影响两份 比如:修改菜品分类那这样就会影响两份数据了
        //将所有的菜品缓存数据清理掉,dish_开头的key
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);

        return Result.success();
    }

    @PostMapping("/status/{status}")
    @ApiOperation(value = "修改菜品状态", notes = "修改菜品状态")
    public Result startOrStop(@RequestParam("id") Long id, @PathVariable("status") Integer status){
        log.info("修改菜品状态：id={}, status={}", id, status);
        dishService.startOrStopDish(id, status);

        //将所有的菜品缓存数据清理掉,dish_开头的key
//        Set keys = redisTemplate.keys("dish_*");
//        redisTemplate.delete(keys);
        clearCache("dish_*");
        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation(value = "根据分类id查询所有菜品列表", notes = "根据分类id查询所有菜品列表")
    public Result<List<Dish>> list(Long categoryId){
        log.info("根据分类id查询所有菜品列表");
        List<Dish> list = dishService.listDishByCategoryId(categoryId);
        return Result.success(list);
    }


    /**
     * 清理缓存数据
     * @param pattern
     */
    private void clearCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
